//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Web;
using DNAide.Core;

namespace DNAide.Web
{
	/// <summary>
	/// Cookies helper methods
	/// </summary>
	public static partial class CookieAide
	{

		/// <summary>
		/// Checks to see if a cookie exists
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public static Boolean Exists(HttpContext currentContext, String name)
		{
			if (currentContext == null) throw new ArgumentException("currentContext");
			if (string.IsNullOrEmpty(name)) throw new ArgumentException("name");
			if (currentContext.Request.Cookies[name] != null)
				return true;
			else
				return false;
		}


		#region Create

		/// <summary>
		/// Creates a new cookie. Note, doesn't add (save) to the response.
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static HttpCookie Create(String name)
		{
			if (string.IsNullOrEmpty(name)) throw new ArgumentException("name");

			HttpCookie retval = new HttpCookie(name);
			return retval;
		}


		/// <summary>
		/// Creates a new cookie. Note, doesn't add (save) to the response.
		/// </summary>
		/// <param name="cookieName"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public static HttpCookie Create(String cookieName, String value)
		{
			HttpCookie retval = Create(cookieName);
			retval.Value = CastAide.AsString(value);
			return retval;
		}


		/// <summary>
		/// Creates a new cookie. Note, doesn't add (save) to the response.
		/// </summary>
		/// <param name="cookieName"></param>
		/// <param name="values"></param>
		/// <returns></returns>
		public static HttpCookie Create(String cookieName, NameValueCollection values)
		{
			HttpCookie retval = Create(cookieName);
			foreach (string name in values)
			{
				retval[name] = values[name];
			}
			return retval;
		}

		#endregion


		#region Save

		/// <summary>
		/// Saves a cookie to the current response. Expires in 100 years.
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="cookieToSave"></param>
		public static void Save(HttpContext currentContext, HttpCookie cookieToSave)
		{
			Save(currentContext, cookieToSave, DateTime.Now.AddYears(100));
		}


		/// <summary>
		/// Saves a cookie to the current response.
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="cookieToSave"></param>
		/// <param name="expires"></param>
		public static void Save(HttpContext currentContext, HttpCookie cookieToSave, DateTime expires)
		{
			if (currentContext == null) throw new ArgumentException("current");
			if (cookieToSave == null) throw new ArgumentException("cookie");
			
			cookieToSave.Expires = expires;
			currentContext.Response.Cookies.Add(cookieToSave);
		}

		#endregion


		#region CreateAndSave

		/// <summary>
		/// Creates and adds to the current response. Expires in 100 years.
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public static HttpCookie CreateAndSave(HttpContext currentContext, String name)
		{
			HttpCookie retval = Create(name);
			Save(currentContext, retval);
			return retval;
		}


		/// <summary>
		/// Creates and adds to the current response. Expires in 100 years.
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public static HttpCookie CreateAndSave(HttpContext currentContext, String name, String value)
		{
			HttpCookie retval = Create(name, value);
			Save(currentContext, retval);
			return retval;
		}


		/// <summary>
		/// Creates and adds to the current response.
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <param name="value"></param>
		/// <param name="expires"></param>
		/// <returns></returns>
		public static HttpCookie CreateAndSave(HttpContext currentContext, String name, String value, DateTime expires)
		{
			HttpCookie retval = Create(name, value);
			Save(currentContext, retval, expires);
			return retval;
		}

		/// <summary>
		/// Creates and adds to the current response. Expires in 100 years.
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <param name="values"></param>
		/// <returns></returns>
		public static HttpCookie CreateAndSave(HttpContext currentContext, String name, NameValueCollection values)
		{
			HttpCookie retval = Create(name, values);
			Save(currentContext, retval);
			return retval;
		}

		/// <summary>
		/// Creates and adds to the current response.
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <param name="values"></param>
		/// <param name="expires"></param>
		/// <returns></returns>
		public static HttpCookie CreateAndSave(HttpContext currentContext, String name, NameValueCollection values, DateTime expires)
		{
			HttpCookie retval = Create(name, values);
			Save(currentContext, retval, expires);
			return retval;
		}

		#endregion


		#region Expire

		/// <summary>
		/// Expires a cookies and writes this to the current response
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="cookieToExpire"></param>
		public static void Expire(HttpContext currentContext, HttpCookie cookieToExpire)
		{
			if (currentContext == null) throw new ArgumentException("currentContext");
			if (cookieToExpire != null)
			{
				Save(currentContext, cookieToExpire, DateTime.Now.AddDays(-1));
			}
		}


		/// <summary>
		/// Expires a cookies and writes this to the current response
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		public static void Expire(HttpContext currentContext, string name)
		{
			if (currentContext == null) throw new ArgumentException("currentContext");
			if (string.IsNullOrEmpty(name)) throw new ArgumentException("name");
			if (Exists(currentContext, name))
			{
				Expire(currentContext, currentContext.Request.Cookies[name]);
			}
		}

		#endregion


		#region GetValue

		/// <summary>
		/// Set cookie value
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <param name="value"></param>
		public static void SetValue(HttpContext currentContext, String name, String value)
		{
			if (currentContext == null) throw new ArgumentException("currentContext");
			if (string.IsNullOrEmpty(name)) throw new ArgumentException("name");
			if (string.IsNullOrEmpty(value)) throw new ArgumentException("value");

			if (Exists(currentContext, name))
			{
				currentContext.Request.Cookies[name].Value = value;
			}
		}

		/// <summary>
		/// Set cookie value
		/// </summary>
		/// <param name="cookie"></param>
		/// <param name="value"></param>
		public static void SetValue(HttpCookie cookie, String value)
		{
			if (cookie == null) throw new ArgumentException("cookie");
			if (string.IsNullOrEmpty(value)) throw new ArgumentException("value");
			
			cookie.Value = value;
		}

		/// <summary>
		/// Set cookie key value
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <param name="key"></param>
		/// <param name="value"></param>
		public static void SetKeyValue(HttpContext currentContext, String name, String key, String value)
		{
			if (currentContext == null) throw new ArgumentException("currentContext");
			if (string.IsNullOrEmpty(name)) throw new ArgumentException("name");
			if (string.IsNullOrEmpty(key)) throw new ArgumentException("key");
			if (string.IsNullOrEmpty(value)) throw new ArgumentException("value");

			if (Exists(currentContext, name))
			{
				currentContext.Request.Cookies[name].Values.Set(key, value);
			}
		}

		/// <summary>
		/// Set cookie key value
		/// </summary>
		/// <param name="cookie"></param>
		/// <param name="key"></param>
		/// <param name="value"></param>
		public static void SetKeyValue(HttpCookie cookie, String key, String value)
		{
			if (cookie == null) throw new ArgumentException("cookie");
			if (string.IsNullOrEmpty(key)) throw new ArgumentException("key");
			if (string.IsNullOrEmpty(value)) throw new ArgumentException("value");

			cookie.Values.Set(key, value);
		}


		/// <summary>
		/// Get cookie value
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public static String GetValue(HttpContext currentContext, String name)
		{
			if (currentContext == null) throw new ArgumentException("currentContext");
			if (string.IsNullOrEmpty(name)) throw new ArgumentException("name");

			string retval = string.Empty;
			if (Exists(currentContext, name))
			{
				retval = currentContext.Request.Cookies[name].Value;
			}
			return retval;
		}

		/// <summary>
		/// Get cookie value
		/// </summary>
		/// <param name="cookie"></param>
		/// <returns></returns>
		public static String GetValue(HttpCookie cookie)
		{
			if (cookie == null) throw new ArgumentException("cookie");

			return cookie.Value;
		}

		/// <summary>
		/// Get cookies values
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		public static NameValueCollection GetValues(HttpContext currentContext, String name)
		{
			if (currentContext == null) throw new ArgumentException("currentContext");
			if (string.IsNullOrEmpty(name)) throw new ArgumentException("name");

			NameValueCollection retval = new NameValueCollection();
			if (Exists(currentContext, name))
			{
				retval = currentContext.Request.Cookies[name].Values;
			}
			return retval;
		}

		/// <summary>
		/// Get cookie values
		/// </summary>
		/// <param name="cookie"></param>
		/// <returns></returns>
		public static NameValueCollection GetValues(HttpCookie cookie)
		{
			if (cookie == null) throw new ArgumentException("cookie");
			return cookie.Values;
		}

		/// <summary>
		/// Get a key value from a cookie
		/// </summary>
		/// <param name="currentContext"></param>
		/// <param name="name"></param>
		/// <param name="key"></param>
		/// <returns></returns>
		public static String GetKeyValue(HttpContext currentContext, String name, String key)
		{
			if (currentContext == null) throw new ArgumentException("currentContext");
			if (string.IsNullOrEmpty(name)) throw new ArgumentException("name");
			if (string.IsNullOrEmpty(key)) throw new ArgumentException("key");
			
			string retval = string.Empty; ;
			if (Exists(currentContext, name))
			{
				if (currentContext.Request.Cookies[name].Values[key] != null)
					retval = CastAide.AsString(currentContext.Request.Cookies[name].Values[key]);
			}
			return retval;
		}

		/// <summary>
		/// Get a key value from a cookie
		/// </summary>
		/// <param name="cookie"></param>
		/// <param name="key"></param>
		/// <returns></returns>
		public static String GetKeyValue(HttpCookie cookie, String key)
		{
			if (cookie == null) throw new ArgumentException("cookie");
			if (string.IsNullOrEmpty(key)) throw new ArgumentException("key");

			string retval = string.Empty; ;
			if (cookie.Values[key] != null)
				retval = CastAide.AsString(cookie.Values[key]);
			return retval;
		}
		#endregion
		


	}
}
